<meta charset="utf-8">
<script src="/bower_components/webcomponentsjs/webcomponents-loader.js"></script>
<script src="/bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="/bower_components/iron-test-helpers/iron-test-helpers.html">
<link rel="import" href="/elements/som-extension-build-failure/som-extension-build-failure.html">
<test-fixture id="basic">
  <template>
    <som-extension-build-failure></som-extension-build-failure>
  </template>
</test-fixture>
<script>
(function() {
  'use strict';

  suite('basic tests', function() {
    var element;

    setup(function() {
      element = fixture('basic');
    });

    test('renders bare minimum', function(done) {
      element.extension = {
        bar: ['baz'],
        builders: [
          {name: 'dummy builder 1', first_failure:0, latest_failure:0},
          {name: 'dummy builder 2', first_failure:0, latest_failure:0},
        ],
        reason: {},
        regression_ranges: []
      };
      element._uberify = (url) => { return url; };
      flush( () => {
        assert.isDefined(element.$$('#builders').textContent.trim());
        done();
      });
    });

    test('failureCountText', function() {
      assert.equal(element._failureCountText({
        latest_failure:0, first_failure:0}), '');

      assert.equal(element._failureCountText({
        latest_failure:1, first_failure:0}), '[2 since first detection]');

      assert.equal(element._failureCountText({
        latest_failure:4, first_failure:0}), '[5 since first detection]');

      assert.equal(element._failureCountText({
        latest_failure:4, first_failure:0, count: 2}),
        '[2 out of the last 5 builds have failed]');
    });

    test('renders count of successive failures', function(done) {
      element.extension = {
        builders: [
          {name: 'dummy builder 1', first_failure:0, latest_failure:3},
          {name: 'dummy builder 2', first_failure:0, latest_failure:4, count:2},
          {name: 'dummy builder 3', first_failure:0, latest_failure:4, count:4},
        ],
        reason: {},
        regression_ranges: []
      };
      flush(() => {
        let items = Polymer.dom(element.root).querySelectorAll('.builder');
        assert.equal(items.length, 3);
        assert.include(items[0].textContent.trim(), '4 since first detection');
        assert.include(items[1].textContent.trim(), '2 out of the last 5 builds have failed');
        assert.include(items[2].textContent.trim(), '4 out of the last 5 builds have failed');
        done();
      });
    });

    test('renders links for test results', function(done) {
      element.extension = {
        reason: {
          'tests': [
            {'test_name': 'best_test'},
          ],
          'step': 'webkit_tests',
        },
      };
      element._uberify = (url) => { return url; };
      flush( () => {
        let item = element.$$('#test_results');
        assert.isNotNull(item);
        assert.isTrue(item.children[0].href.endsWith(
                'dashboards/flakiness_dashboard' +
                '.html#tests=best_test&testType=webkit_tests'));
        done();
      });
    });

    test('renders links for truncated test results', function(done) {
      element.extension = {
        reason: {
          'tests': [
          {'test_name': 'best_test'},
          {'test_name': 'second_best_test'},
          ],
          'step': 'webkit_tests',
          'num_failing_tests': 10
        },
      };
      element._uberify = (url) => { return url; };
      flush( () => {
        let header = element.$$('.header');
        assert.isNotNull(header);
        assert.equal(header.innerText, "10 tests failed, first 2:")
        done();
      });
    });

    test('renders links for code search', function(done) {
      element.extension = {
        reason: {
          'tests': [
            {'test_name': 'best_test'},
          ],
        },
      };
      element._uberify = (url) => { return url; };
      flush( () => {
        let item = element.$$('#test_results');
        assert.isNotNull(item);
        assert.isTrue(item.children[1].href.endsWith('search/?q=best_test'));
        done();
      });
    });

    test('renders links for artifacts', function(done) {
      element.extension = {
        reason: {
          'tests': [
            {
              'test_name': 'best_test',
              'artifacts': [
                {
                  name: 'sadness',
                  location: 'https://foo',
                },
                 {
                  name: 'sadness',
                  location: 'https://bar',
                },
              ],
            },
          ],
        },
      };
      element._uberify = (url) => { return url; };
      flush( () => {
        let artifacts = element.$$('.artifacts');
        assert.isNotNull(artifacts);
        // 2 LI's + one dom-repeat
        assert.equal(3, artifacts.childElementCount);
        done();
      });
    });

    test('renders links for code search, java test', function(done) {
      element.extension = {
        reason: {
          'tests': [
            {'test_name': 'test.foo.bar.TestClass#testThatJavaHasTheBestNames',},
          ],
        },
      };
      flush( () => {
        let item = element.$$('#test_results');
        assert.isNotNull(item);
        assert.isTrue(item.children[1].href.endsWith(
            'search/?q=test.foo.bar.TestClass%20' +
            'function%3AtestThatJavaHasTheBestNames'));
        done();
      });
    });

    test('renders suspect CLs', function(done) {
      element.extension = {
        reason: {
          'tests': [
            {'test_name': 'best_test'},
          ],
        },
        suspected_cls: [
          {revision: '1234', confidence: 0, commmit_position: 123},
        ],
      };
      flush(() => {
        let item = element.$$('#finditResults');
        assert.isNotNull(item);
        assert.equal(item.getElementsByClassName('suspect-cl-link')[0].href,
          'https://crrev.com/1234');
        done();
      });
    });

    test('renders Findit v2 culprits', function(done) {
      element.extension = {
        culprits: [
          {commit: {id: '1234', host: 'gitiles.host.com', project:'project/name', commmit_position: 123}},
        ],
      };
      flush(() => {
        let item = element.$$('#finditResults');
        assert.isNotNull(item);
        assert.equal(item.getElementsByClassName('suspect-cl-link')[0].href,
          'https://gitiles.host.com/project/name/+/1234');
        done();
      });
    });

    test('removes duplicate Findit v2 culprits', function() {
      element.extension = {
        culprits: [
          {commit: {id: '1234', host: 'gitiles.host.com', project:'project/name', commmit_position: 123}},
          {commit: {id: '1234', host: 'gitiles.host.com', project:'project/name', commmit_position: 123}},
        ],
      };

      assert.equal(element._culprits.length, 1);
    });

    test('removes duplicate suspect CLs', function() {
      element.extension = {
        reason: {
          'tests': [
            {'test_name': 'best_test'},
          ],
        },
        suspected_cls: [
          {revision: '1234', confidence: 0, commmit_position: 123},
          {revision: '1234', confidence: 0, commmit_position: 123},
          {revision: '12345', confidence: 0, commmit_position: 125},
          {revision: '1234', confidence: 0, commmit_position: 123},
        ],
      };

      assert.equal(element._suspectedCls.length, 2);
    });
  });
})();
</script>
